


package yf.mall.controller.app;

import com.yf.annotation.LoginUserId;
import com.yf.request.SearchParam;

import java.io.IOException;
import java.util.List;

import com.yf.response.RRException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import yf.mall.entity.AppCategoryEntity;
import yf.mall.entity.KolProductEntity;
import yf.mall.service.app.AppCategoryService;
import yf.mall.vo.KolProductVo;
import yf.mall.service.app.KolProductService;
import com.yf.response.R;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.web.multipart.MultipartFile;
import yf.mall.enums.KolProductAssuredEnum;
import yf.mall.enums.KolProductSxtEnum;
import yf.mall.enums.KolProductSharableEnum;
import yf.mall.enums.KolProductRecommendEnum;
import yf.mall.vo.app.product.KolProductSumVo;
import yf.mall.vo.app.request.ProductNotifyRequest;

@RequestMapping("/kol/product")
public class KolProductController {
    @Autowired
    private KolProductService kolProductService;

    @RequestMapping("/list")
    @RequiresPermissions("kol:product:list")
    public R list(@RequestBody SearchParam<KolProductVo> params) {
        PageHelper.startPage(params.getPage(), params.getLimit()).setOrderBy(params.getOrder());
        List<KolProductVo> list = kolProductService.findVo(params.getEntity());
        return R.ok(new PageInfo(list));
    }

    @RequestMapping("/down")
    @RequiresPermissions("kol:product:list")
    public void down(@RequestBody SearchParam<KolProductVo> params, HttpServletResponse response) {
        List<KolProductSumVo> list = kolProductService.sum();
        kolProductService.downLoadSum(response, list);
    }

    @RequestMapping("/upload")
    public void upload(MultipartFile file) {
        try {
            kolProductService.upload(file.getInputStream());
        } catch (IOException e) {
            throw new RRException("上传的文件有误!");
        }
    }


    @RequestMapping("/info/{id}")
    @RequiresPermissions("kol:product:info")
    public R info(@PathVariable("id") Long id) {
        KolProductVo vo = kolProductService.getVo(id);
        return R.ok(vo);
    }

    @RequestMapping("/save")
    @RequiresPermissions("kol:product:save")
    public R save(@RequestBody KolProductVo vo) {
        kolProductService.saveVo(vo);
        return R.ok(vo);
    }

    @RequestMapping("/update")
    @RequiresPermissions("kol:product:update")
    public R update(@RequestBody KolProductVo vo) {
        kolProductService.updateVo(vo);

        return R.ok(vo);
    }

    @RequestMapping("/delete")
    @RequiresPermissions("kol:product:delete")
    public R delete(Long[] ids) {
        kolProductService.deleteBatch(ids);

        return R.ok();
    }

    @RequestMapping("/all")
    public R all(String order) {
        PageHelper.orderBy(order != null ? order : "id desc");
        List<KolProductVo> list = kolProductService.findVo(null);
        return R.ok(list);
    }

    @RequestMapping("/dmz/options")
    public R options() {
        HashMap<String, Object> options = new HashMap();
        options.put("assuredOptions", KolProductAssuredEnum.names());
        options.put("sxtOptions", KolProductSxtEnum.names());
        options.put("sharableOptions", KolProductSharableEnum.names());
        options.put("recommendOptions", KolProductRecommendEnum.names());
        //找到与产品相关的分类
        List<AppCategoryEntity> appTypeOptions =  categoryService.findForAppProduct();
        options.put("appTypeOptions",appTypeOptions.stream().collect(Collectors.toMap(AppCategoryEntity::getType,Function.identity())));
        List<AppCategoryEntity> wxTypeOptions =  categoryService.findForWxProduct();
        options.put("wxTypeOptions",wxTypeOptions.stream().collect(Collectors.toMap(AppCategoryEntity::getType, Function.identity())));
        List<AppCategoryEntity> materialTab = categoryService.findForProductMaterialType();
        options.put("materialTabOptions",materialTab.stream().collect(Collectors.toMap(AppCategoryEntity::getType,Function.identity())));
        return R.ok(options);
    }

    @Resource
    private AppCategoryService categoryService;


    /***********自定义开始***********/


    @RequestMapping("/notify-dy-product")
    public R notifyDyProduct(@LoginUserId Long userId, ProductNotifyRequest request) {
        if (request == null || CollectionUtils.isEmpty(request.getProductIds()) || request.getType() == null) {
            return R.error("参数异常");
        }
        if (request.getProductIds().size() > 10) {
            return R.error("一次性最多同步10个商品");
        }
        kolProductService.notifyDyProductToDb(userId, request);
        return R.ok();
    }

    @RequestMapping("/req-dy-product/{id}")
    @RequiresPermissions("kol:product:info")
    public R reqDyProduct(@PathVariable Long id) {
//        Long userId = 254893L;//获取抖音产品数据 需要授权 这里固定指定一个人
        KolProductEntity kolProduct = kolProductService.reqDyProduct( id);
        KolProductVo kolProductVo = new KolProductVo(kolProduct);
        return R.ok(kolProductVo);
    }
}
